//
//  LanguageVIew.swift
//  JiaoZiTaxi
//
//  Created by Hu Gang on 2022/12/6.
//

import SwiftUI

struct LanguageView: View {
    @ObservedObject var vm = ViewModel.shared
    @Environment(\.dismiss) var dismiss
    @Binding var showMe : Bool

    var body: some View {
        GeometryReader { geometry in
            VStack {
                Button {
                    DispatchQueue.main.async {
                        vm.language = .Chinese
                    }
                } label: {
                    HStack {
                        Text("中文")
                        if vm.language == .Chinese {
                            Image(systemName: "checkmark.circle.fill")
                                .foregroundColor(.green)
                        }
                    }.padding()
                        .frame(width:geometry.size.width - 60)
                        .background(
                            RoundedRectangle(cornerRadius: 10.0)
                                .fill(Color.themeColor()))
                }

                Button {
                    DispatchQueue.main.async {
                        vm.language = .English
                    }

                } label: {
                    HStack {
                        Text("english")
                        if vm.language == .English {
                            Image(systemName: "checkmark.circle.fill")
                                .foregroundColor(.green)
                        }
                    }.padding()
                        .frame(width:geometry.size.width - 60)
                        .background(
                            RoundedRectangle(cornerRadius: 10.0)
                                .fill(Color.themeColor()))
                }

                Button {
                    DispatchQueue.main.async {
                        vm.language = .Laos

                    }
                } label: {
                    HStack {
                        Text("ເມືອງລາວ")
                        if vm.language == .Laos {
                            Image(systemName: "checkmark.circle.fill")
                                .foregroundColor(.green)
                        }
                    }.padding()
                        .frame(width:geometry.size.width - 60)
                        .background(
                            RoundedRectangle(cornerRadius: 10.0)
                                .fill(Color.themeColor()))
                }

                Button {
                    self.showMe = false
                    reportToken()
                } label: {
                    Text("确定").foregroundColor(Color.themeColor())
                }.padding()

            }
            .padding(.top,30)
            .foregroundColor(.white)
                .offset(x:30)
        }.background(.white)
            .cornerRadius(10)
    }

    func reportToken() {
        let token = vm.deviceToken
        let openId = vm.passenger.openId
        logger.info("updateToken:\(openId) \(token)")
        if token.count > 0 && openId.count > 0 {
            Api.updateToken(openId: openId, token: token) { r in
                switch r {
                    case .failure(let error):
                        logger.info("updateToken error:\(error)")

                    case .success(let data):
                        logger.info("updateToken success:\n\(data)")
                }
            }
        }
    }
}

struct LanguageView_Previews: PreviewProvider {
    static var previews: some View {
        LanguageView(showMe: .constant(true))
    }
}
